
import Head from 'next/head'

<Head>
  <script>
    {
      `(function() {
         var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?e60fb290e204e04c5cb6f79b0ac1e697";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
       })();`
    }
  </script>
</Head>

![LangChain](https://pica.zhimg.com/50/v2-56e8bbb52aa271012541c1fe1ceb11a2_r.gif)




Anthropic聊天模型
=============

本教程将介绍如何使用Anthropic聊天模型入门。

```python
from langchain.chat_models import ChatAnthropic
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    HumanMessagePromptTemplate,
)
from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)

```

```python
chat = ChatAnthropic()

```

```python
messages = [
    HumanMessage(content="Translate this sentence from English to French. I love programming.")
]
chat(messages)

```

```python
AIMessage(content=" J'aime programmer. ", additional_kwargs={})

```

`ChatAnthropic`支持异步和Stream流[#](#chatanthropic-also-supports-async-and-streaming-functionality "Permalink to this headline")
----------------------------------------------------------------------------------------------------------------------------------------------------------------

```python
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

```

```python
await chat.agenerate([messages])

```

```python
LLMResult(generations=[[ChatGeneration(text=" J'aime la programmation.", generation_info=None, message=AIMessage(content=" J'aime la programmation.", additional_kwargs={}))]], llm_output={})

```

```python
chat = ChatAnthropic(streaming=True, verbose=True, callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]))
chat(messages)

```

```python
 J'adore programmer.

```

```python
AIMessage(content=" J'adore programmer.", additional_kwargs={})

```

